class Article < ActiveRecord::Base
  belongs_to :user
  belongs_to :topic

  has_many_kindeditor_assets :attachments, :dependent => :destroy

  default_scope { order('created_at DESC') }
  default_scope { where(is_published: true, is_deleted: false) }
  default_scope { where("created_at < ?", Time.now) }

  validates :title,
            presence:  {:message => '标题不能为空'},
            :length => {:minimum => 2, :maximum => 50, :message => "2～50字"}

  validates :content,
            presence:  {:message => '内容不能为空'},
            :length => {:minimum => 5, :maximum => 10000, :message => "5～10000字"}


  def previous
    self.class.where(["created_at < ?", created_at]).where(topic_id: topic_id).first
  end

  def next
    self.class.where(["created_at > ?", created_at]).where(topic_id: topic_id).last
  end

  private

end


